package sg.edu.nus.iss.team2s.jass.dao;

import java.util.*;

import sg.edu.nus.iss.team2s.jass.dto.*;
import sg.edu.nus.iss.team2s.jass.util.*;

/*
 * <p>The <strong>JobAdvertDAO</strong> is to manage the communication between controller and the data storage for job advert object. 
 * This class implements the functionality of retrieve,update,delete for jobadvert from data storage.</p>

 * @author LU ZHIXIAN
 * @version 1.1
 */

public class JobAdvertDAO extends AbstractPropertiesDAO {

public static final String FILENAME = "JobAdvert";
	
	//get the fields, need to be overridden in the subclass
//	@Override
//	protected  String[] getFields(){
//		return new String[]{"jobAvertId","jobTitle","jobDesc","techSkillList","manageSkillList","experience","salaryRange","startDate","location","companyId","department","advertiserId","postDate","updateDate","status"};
//	}
	
/**
 * This is to add new JobAdvert {@link sg.edu.nus.iss.team2s.jass.dao.JobAdvert} into data storage
 
 * 
 * @param JobAdvert object , the jobadvertId should be empty
 * @return void
 * @exception  DAOException due to retrieve data error from the database or failed for some validation
 *
 */
	@Override
	public void add(Object obj) throws DAOException {
		JobAdvert jobAdvert=(JobAdvert) obj;

		
		//if (jobAdvert.getJobAdvertId().equalsIgnoreCase("")){
		//	throw new DAOException("Job Advert Id is empty");	
		//}
		
		if (jobAdvert.getCompanyId().equals("") || jobAdvert.getAdvertiserId().equals("")){
			throw new DAOException("Either the Company ID or Advertiser ID is NULL.");	
		}
		
		if (jobAdvert.getJobTitle().equals("") || jobAdvert.getJobDesc().equals("")){
			throw new DAOException("Either the job advert title or description is NULL.");	
		}
		
		//check duplicate
		
		JobAdvert temp;
		
		try{
			temp=(JobAdvert)get(jobAdvert.getJobAdvertId());
		}catch(DAOException exp)
		{
			if (exp.getMessage().equalsIgnoreCase("NO Job Advert Record Found!"))
			temp=null;
			else
				throw exp;
		}
		
		
		if (temp!=null){
			throw new DAOException("Job Advert Id is duplicated!");	
		}
		
		List<Object> arrList= list();
		
		arrList.add(obj);
		
		update(arrList);
			
	}
	/**	
	 * This is to get an existing JobAdvert {@link sg.edu.nus.iss.team2s.jass.dao.JobAdvert} from data storage
	 
	 * 
	 * @param key , the jobadvertId  of the JobAdvert
	 * @return an JobAdvert Object
	 * @exception  DAOException due to retrieve data error from the database or no record found.
	*/ 
	@Override
	public Object get(String key) throws DAOException {
		
		ArrayList<Object> arrList=(ArrayList<Object>)list();
		
		for(int i=0; i < arrList.size(); i++)  {
			JobAdvert jobAdvert=(JobAdvert)arrList.get(i);
		    if (jobAdvert.getJobAdvertId().equalsIgnoreCase(key)){
		    	return jobAdvert;	
		    }
			
		}
		
		throw new DAOException("NO Job Advert Record Found!");
	}

	/**	
	 * This is to get current JobAdvert {@link sg.edu.nus.iss.team2s.jass.dao.JobAdvert} list from data storage
	 
	 * 
	 * @param null
	 * @return an List of JobAdvert Object
	 * @exception  DAOException due to retrieve data error from the database.
	*/ 
	
	@Override
	public List<Object> list() throws DAOException {

		List<Object> list;
		try{
			list= retrieve(JobAdvertDAO.FILENAME );	
		}
		catch(Exception exp){
			throw new DAOException(exp.getMessage(),exp.getCause());
			
		}
		return list;
	}
	
	/**	
	 * This is to update JobAdvert list {@link sg.edu.nus.iss.team2s.jass.dao.JobAdvert} into data storage
	 * All existing Jobadverts in the data storage will be replaced with new list.
	 * 
	 * @param an List of JobAdvert Object
	 * @return void
	 * @exception  DAOException due to retrieve data error from the database.
	*/ 
	
	@Override
	public void update(List<Object> list) throws DAOException {

		try{
			flush(list, JobAdvertDAO.FILENAME);	
		}
		catch(Exception exp){
			throw new DAOException(exp.getMessage(),exp.getCause());
			
		}
	}
	
	/**	
	 * This is to update a particular JobAdvert list {@link sg.edu.nus.iss.team2s.jass.dao.JobAdvert} into data storage
	 
	 * 
	 * @param an JobAdvert Object
	 * @return void
	 * @exception  DAOException due to retrieve data error from the database.
	*/ 
	
	public void updateJobAdvert(Object obj) throws DAOException{
	ArrayList<Object> arrList=(ArrayList<Object>)list();
		
	 boolean ret=false;
	 
		for(int i=0; i < arrList.size(); i++)  {
			JobAdvert jobAdvert=(JobAdvert)(arrList.get(i));
		    if (jobAdvert.getJobAdvertId().equalsIgnoreCase(((JobAdvert)obj).getJobAdvertId())){
				arrList.remove(i);
		        arrList.add(i, obj);
		    	update(arrList);
		    	ret=true;
		    	break;
		    }
			
		}
		
		if (ret==false){
		throw new DAOException("NO Job Advert Record Found!");}	
	}
	
	/**	
	 * This is to update a particular JobAdvert list {@link sg.edu.nus.iss.team2s.jass.dao.JobAdvert} into data storage
	 * Set status to Withdraw status  {@link sg.edu.nus.iss.team2s.jass.dao.JobAdvert.WITHDRAW}. 
	 * 
	 * @param an JobAdvert Object
	 * @return void
	 * @exception  DAOException due to retrieve data error from the database.
	*/ 
	public void withdrawJobAdvert(Object obj)throws DAOException{
		JobAdvert jobAdvert=(JobAdvert) obj;
		
		jobAdvert.setStatus(JobAdvert.WITHDRAW);
		
		updateJobAdvert(obj);
	}
	
	/**	
	 * This is to update a particular JobAdvert list {@link sg.edu.nus.iss.team2s.jass.dao.JobAdvert} into data storage
	 * Set status to Deleted Status {@link sg.edu.nus.iss.team2s.jass.dao.JobAdvert.DELETED}
	 * 
	 * @param an JobAdvert Object
	 * @return void
	 * @exception  DAOException due to retrieve data error from the database.
	*/ 
	public void deleteJobAdvert(Object obj) throws DAOException{
		
		JobAdvert jobAdvert=(JobAdvert) obj;
		
		jobAdvert.setStatus(JobAdvert.DELETED);
		
		updateJobAdvert(obj);
		
	}
}
